const UserToken = require('../models/usertoken')

async function createToken(username, userType, token, expireTime) {
  return await UserToken.create({
    username,
    userType,
    token,
    expireTime,
    updateTime: new Date(),
  })
}
async function getToken(username) {
  return await UserToken.findOne({ where: { username } })
}
async function updateToken(username, token) {
  return await UserToken.update(
    { token, updateTime: new Date() },
    { where: { username } }
  )
}
async function clearToken(username) {
  return await UserToken.destroy({ where: { username } })
}
async function getAllToken() {
  return await UserToken.findAll()
}

module.exports = {
  createToken,
  getToken,
  updateToken,
  clearToken,
  getAllToken,
}
